#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2023/8/16 12:00
# @Author  : huidong.bai
# @File    : ServiceManager.py
# @Software: PyCharm
# @Mail    : MasterBai2018@outlook.com

import multiprocessing
import threading
from multiprocessing import Queue, Event


class ServiceManager(multiprocessing.Process):
    def __init__(self, server_name, server, config, lang, queue: Queue, event: Event, car_type=None):
        super().__init__()
        self.server_name = server_name
        self.server = server
        self.config = config
        self.lang = lang
        self.queue = queue
        self.event = event
        self.car_type = car_type

    def run(self) -> None:
        try:
            self.server.load_lib()
            self.event.wait()
            if self.car_type is not None:
                ret = self.server.start(self.config, self.lang, self.car_type)
            else:
                ret = self.server.start(self.config, self.lang)
            self.queue.put(ret)
            threading.Event().wait()
        except KeyboardInterrupt:
            pass

    def stop(self) -> int:
        return self.server.stop()
